package com.example.wyz.classplus.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;

import com.example.wyz.classplus.R;

import butterknife.BindView;

/**
 * Created by 10153 on 2017/5/31.
 */

public abstract class BaseSwipeRefreshActivity <P extends BasePresenter> extends BaseActivity<P>
        implements IBaseSwipeRefreshView{

    @BindView(R.id.swipe_refresh_layout) SwipeRefreshLayout mRefreshLayout;

    protected abstract void onRefreshStarted();

    protected abstract void onLoadMoreStarted();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initSwipeRefreshLayout();
    }

    @Override
    public void getDataFinish() {
        hideRefresh();
    }

    @Override
    public void showRefresh() {
        mRefreshLayout.setRefreshing(true);
    }

    @Override
    public void hideRefresh() {
        mRefreshLayout.postDelayed(new Runnable() {

            @Override
            public void run() {
                if(mRefreshLayout != null){
                    mRefreshLayout.setRefreshing(false);
                }
            }
        }, 1000);
    }

    protected boolean isRefreshing(){
        return mRefreshLayout.isRefreshing();
    }

    private void initSwipeRefreshLayout(){
        mRefreshLayout.setColorSchemeResources(
                R.color.colorPrimary, R.color.colorPrimaryDark, R.color.colorAccent);
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

            @Override
            public void onRefresh() {
                onRefreshStarted();
            }
        });
    }
}
